package cn.tedu.tmall.admin.mall.controller;

import cn.tedu.tmall.admin.mall.pojo.param.CategoryAddParam;
import cn.tedu.tmall.admin.mall.pojo.po.CategoryPO;
import cn.tedu.tmall.admin.mall.pojo.vo.CategoryTreeTempVO;
import cn.tedu.tmall.admin.mall.pojo.vo.CategoryTreeVO;
import cn.tedu.tmall.admin.mall.service.ICategoryService;
import cn.tedu.tmall.common.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

@RestController
@Api(tags = "商城管理端-商品分类管理")
public  class MallCategoryController {

    @Autowired
    private ICategoryService categoryService;

    @ApiOperation("增加商品分类")
    @PostMapping("category/add")
    public JsonResult addCategory(@Validated CategoryAddParam categoryAddParam){
        categoryService.addCategory(categoryAddParam);
        return JsonResult.ok();
    }

    @ApiOperation("删除商品分类")
    @PostMapping("category/del")
    public JsonResult delCategory(@Validated CategoryAddParam categoryAddParam){
        return JsonResult.ok();
    }

    /**
     * 返回商品分类树
     * 实现方式是全部加载
     * @return
     */
    @ApiOperation("商品分类树")
    @GetMapping("category/tree")
    public JsonResult categoryTree(){
        //categoryService.addCategory(categoryPO);
        List<CategoryTreeVO> tree = categoryService.treeCategory();
        return JsonResult.ok(tree);
    }

    @ApiOperation("商品分类树-cache")
    @GetMapping("category/cache/tree")
    public JsonResult categoryCacheTree(){
        //categoryService.addCategory(categoryPO);
        List<CategoryTreeVO> tree = categoryService.treeCategoryFromCache();
        return JsonResult.ok(tree);
    }

    @ApiOperation("商品分类树-cache-顶级类别-延迟加载")
    @GetMapping("category/tree/cache/top")
    public JsonResult categoryTreeCacheTop(){
        List<CategoryTreeVO> tree = categoryService.topTreeCacheCategory();
        return JsonResult.ok(tree);
    }

    @ApiOperation("商品分类树-cache-通过类别id查询子类别-延迟加载")
    @GetMapping("category/tree/cache/children/{id}")
    public JsonResult categoryTreeCacheChildren(@PathVariable("id") Long id){
        List<CategoryTreeVO> tree = categoryService.childrenTreeCacheCategory(id);
        return JsonResult.ok(tree);
    }

    @ApiOperation("商品分类树-顶级类别-延迟加载")
    @GetMapping("category/tree/top")
    public JsonResult categoryTreeTop(){
        List<CategoryTreeVO> tree = categoryService.topTreeCategory();
        return JsonResult.ok(tree);
    }

    @ApiOperation("商品分类树-通过类别id查询子类别-延迟加载")
    @GetMapping("category/tree/children/{id}")
    public JsonResult categoryTreeChildren(@PathVariable("id") Long id){
        List<CategoryTreeVO> tree = categoryService.childrenTreeCategory(id);
        return JsonResult.ok(tree);
    }



}
